<?php
/**
 * Created by PhpStorm.
 * User: spatra
 * Date: 14-11-8
 * Time: 上午10:10
 */

namespace Student;

use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Input;

class LessonController extends \LessonController
{
    public function index()
    {
        $condName = Input::get('cond', 'all');
        $respData = $this->getIndexByCond($condName);

        return \Response::json($respData);
    }

    protected function getIndexByCond($condName){

        $currIndexMethod = $condName. 'Index';
        if ( method_exists($this, $currIndexMethod) ) {
            return $this->$currIndexMethod();
        }
        return $this->allIndex();
    }

    protected function allIndex()
    {
        return \Lesson::all();
    }

    protected function mineIndex()
    {
        $studentId = \Auth::user()['id'];
        $lessonIds = \Lesson_Student::where('student_id', $studentId)
            ->where('status_id', '!=', 2)->get()->toArray();

        $lessonIds = array_fetch($lessonIds, 'lesson_id');
        $lessonList = \Lesson::findMany($lessonIds)->toArray();
        return $lessonList;
    }

    protected function exitIndex()
    {
        $studentId = \Auth::user()['id'];
        $lessonIds = \Lesson_Student::where('student_id', $studentId)
            ->where('status_id', 1)->get()->toArray();

        $lessonIds = array_fetch($lessonIds, 'lesson_id');
        $lessonList['other'] = \Lesson::findMany($lessonIds)->toArray();

        $lessonIds = \Lesson_Student::where('student_id', $studentId)
            ->where('status_id', 3)->get()->toArray();

        $lessonIds = array_fetch($lessonIds, 'lesson_id');
        $lessonList['exit'] = \Lesson::findMany($lessonIds)->toArray();

        return $lessonList;
    }

    protected function otherIndex()
    {
        $studentId = \Auth::user()['id'];
        $lessonIds = \Lesson_Student::where('student_id', $studentId)
            ->get()->toArray();

        $lessonIds = array_fetch($lessonIds, 'lesson_id');
        $lessonList['other'] = \Lesson::whereNotIn('id', $lessonIds)->get()->toArray();

        $lessonIds = \Lesson_Student::where('student_id', $studentId)
            ->where('status_id', 2)
            ->get()->toArray();

        $lessonIds = array_fetch($lessonIds, 'lesson_id');
        $lessonList['apply'] = \Lesson::findMany($lessonIds)->toArray();
        return $lessonList;
    }




}